"""
@author: Sergiy Tsymbalyuk
"""

from datetime import date, timedelta

from pylons import request
from sqlalchemy import func

from chowa.lib.base import render, Session
from chowa.model.abstract import Person

class Birthdays(object):
    def __init__(self, options={}):
        pass
    
    def render(self):
        one_day = timedelta(days=1)
        today = date.today()
        yesterday = today - one_day
        tomorrow = today + one_day
        after_tomorrow = tomorrow + one_day
        query = Session.query(Person)
        birthdays = lambda _date: query.filter(Person.is_insider == True) \
                                       .filter(func.day(Person.born) == _date.day) \
                                       .filter(func.month(Person.born) == _date.month).all()
        extra_vars = {
            'yesterday_birthdays': birthdays(yesterday),
            'today_birthdays': birthdays(today),
            'tomorrow_birthdays': birthdays(tomorrow),
            'after_tomorrow_birthdays': birthdays(after_tomorrow),
            'yesterday': yesterday,
            'today': today,
            'tomorrow': tomorrow,
            'after_tomorrow': after_tomorrow,
        }
        return render('/social/birthdays.html', extra_vars=extra_vars)
